// /////////////////////////////////////////////////////////////////////////////
//
// Name:            HasEvents.cpp
// Author:          Michael Bartsch (ZCCdark203)
//
// Desc :           The HasEvents class provides a base for classes that need a
//                  decentralized event system. By deriving from this class,
//                  the derived class gains all needed methods for the
//                  management of a decentralized event system.
//
// License:         Copyright (C) 2009 Michael Bartsch and Contributors
//
//                  This program is free software: you can redistribute it
//                  and/or modify it under the terms of the zlib/libpng License.
//                  See main.cpp for conditions of distribution and use.
//
// /////////////////////////////////////////////////////////////////////////////

// Include files
#include "HasEvents.h"


// HasEvents class
// HasEvents constructor.
HasEvents::HasEvents()
{
}

// HasEvents deconstructor.
HasEvents::~HasEvents()
{
    removeEventSlots();
}

// Creates a new event slot.
bool HasEvents::createEventSlot(const std::string &slotName)
{
    // Check if the given slot doesnt already exist.
    std::map<std::string, EventSlot>::iterator itSlots = mEventSlots.find(slotName);

    if(itSlots != mEventSlots.end())
      return false;

    // Create a new event slot.
    EventSlot slot;
    mEventSlots[slotName] = slot;

    return true;
}

// Emits an event to the given event slot.
bool HasEvents::emitEvent(const std::string &slotName, void* p)
{
    // Get the given event slot.
    std::map<std::string, EventSlot>::iterator itSlots = mEventSlots.find(slotName);

    if(itSlots == mEventSlots.end())
      return false;

    EventSlot &slot = itSlots->second;

    // Emit event.
    slot.emit(p);
    return true;
}

// Removes all event slots.
void HasEvents::removeEventSlots()
{
    mEventSlots.clear();
}

// Removes the given event slot.
bool HasEvents::removeEventSlot(const std::string &slotName)
{
    // Get the given event slot.
    std::map<std::string, EventSlot>::iterator itSlots = mEventSlots.find(slotName);

    if(itSlots == mEventSlots.end())
      return false;

    // Remove it.
    mEventSlots.erase(itSlots);
    return true;

}

// End of File
